/*
 *
 * @ProjectName:    my_umi_app
 * @Package:
 * @ClassName:      layout_view_model
 * @Description:    用于管理菜单数据
 * @Author:         SuPeiSen
 * @CreateDate:     2020/12/21 10:32 下午
 * @UpdateDate:     2020/12/21 10:32 下午
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

import { makeAutoObservable, runInAction } from 'mobx';
import { Requests } from '@/utils/requests';
import { ApiPath } from '@/config/api_config';
import { MenuDataItem } from '@ant-design/pro-layout';
import { MenuItemModel, TreeNodeConfig } from '@/type/menu_type';

export class MenuDataViewModel {
  private menuData?: Array<MenuItemModel>;
  private loadingStatus = false;
  private pathNameStr = '/admin';
  constructor() {
    makeAutoObservable(this);
  }

  public get pathName() {
    return this.pathNameStr;
  }

  public set pathName(name) {
    this.pathNameStr = name;
  }

  get layoutMenu(): Array<MenuDataItem> {
    // 递归获取菜单
    const menuItems = (arr: Array<MenuItemModel>) => {
      let menuArr: Array<MenuDataItem> = [];
      arr.forEach((menu) => {
        if (menu.children !== undefined) {
          const configs = menuItems(menu.children);
          return menuArr.push({
            path: menu.menu_path,
            name: menu.menu_name,
            id: menu.id,
            children: configs,
            // icon: ()=>{
            //   return <StarFilled />
            // },
          });
        }
        menuArr.push({
          path: menu.menu_path,
          name: menu.menu_name,
          id: menu.id,
        });
      });
      return menuArr;
    };
    if (this.menuData === undefined) {
      return [];
    }
    return menuItems(this.menuData!);
  }

  get treeNode() {
    // 递归获取菜单
    const treeNodeItems = (arr: Array<MenuItemModel>) => {
      // log("打印递归结果")
      let treeNodeArr: Array<TreeNodeConfig> = [];
      arr.forEach((menu) => {
        if (menu.children !== undefined) {
          const configs = treeNodeItems(menu.children);
          return treeNodeArr.push({
            id: menu.id,
            title: menu.menu_name,
            value: menu.id.toString(),
            children: configs,
          });
        }
        treeNodeArr.push({
          title: menu.menu_name,
          id: menu.id,
          value: menu.id.toString(),
        });
      });
      return treeNodeArr;
    };
    if (this.menuData === undefined) {
      return [];
    }
    return treeNodeItems(this.menuData!);
  }

  get loading() {
    return this.loadingStatus;
  }

  async updateMenuData() {
    // responseModel是一个统一返回对象
    const responseModel = await Requests.defaults.http<Array<MenuItemModel>>(
      'get',
      ApiPath.getMenu,
      {
        callBack: {
          init: () => {
            runInAction(() => {
              this.loadingStatus = true;
            });
          },
          success: (response) => {
            runInAction(() => {
              this.menuData = response.data.data;
            });
          },
          finally: () => {
            runInAction(() => {
              this.loadingStatus = false;
            });
          },
        },
      },
    );
  }
}

export const menuDataModel = new MenuDataViewModel();
